package com.yh.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.yh.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000117685212";

    // 商户私钥，您的PKCS8格式RSA2私钥
    public static String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCI8GCFJnuc2tysQuAyCaGSnC/QkTRZSKadn7hI9WukycwuFwhf59M5+7+ggei9Iu0x0Evl0EbLXc8r5SIqm7XjLKabv0tHQYDaQLsKwsYG0kZmTxz9OXEgPujdwn75Vs41so0ylyOgcV/gCa5nhJA9AuSj4G8EA/Y7YEFqvseGh0YtJwoqGnnSstCn0t2UcaGb7AqmtLHfBvpTzurr9ULWlbFx5+c29ZQcqFmWqQTtnZw9eqI7e6g0cTjo0HJ+mzBZqGIsMgqkeELx1ngB9H23izAyDehm8Cy4TkXfaiTAzIpsL1P5sehnJuNjokBhjPDagwiOpAZxEZq7QmNurmCTAgMBAAECggEAfREFJjaKaSyepJmDh0+yiGMrbFJhvDWVLpqkRcYXpO5FE8cnd+gTy344etCLDsqqVXeHCgJZ1fz8bHud48pIqY2rMM3aY97YXv0FlwNitvIvJ9uTf1aKEkhCh97N9s2oEfTb7luJR0ykospUxeiVj5Nn3J/q5ID1zy7PMD3DnwHwGT4mHDAD9bSFUPsmVQyX4nRnfibA0KUmRzGDHOEZeaSDLH6c+EwKAVUz9jWoHAK1gLSVyyJPmLu1pmsY33av98J09/eNu6woj516Cw8qHtaa2bTay84u4OXC31vAh03lNzoVzICV6+cPdWjsthwl+f2nKlwCUqWk1LL4j1dd6QKBgQDLKgMPjQgf145QKXOveZfucwc+fG5IG/Il53DvdnWDkgA4Lv9djL2kjXHOFKH2btB06E9iXG8i8uDbIWdvUrdW/ZuwVa1+WCRrCZZYdQyEMRMLTP+dFoIl/DLZ7de+u44o4G0RgxP9/Q+YP9X6DDPRLp3ntJwyxSHD/R92oDxt5QKBgQCsjVENgx9g7H3mVeI54x35UuJVjYwpop57LYsKB3AgQjOe9F3LSHeyL+dIQ1u38WrSuj85qYsMWoXxDpbTjrUxlFMXTcM8wxVO7tL2d67KWHvxhJ+ezX98g7mU+PA/0FOoKtLUIIK2Tjwlm2yQPn8ooq3/QXpKEsglbfcA3T5tFwKBgCVFbNBSiDYHPoV4RU/g172X/rE49KZn8hGAFxjnO4HKrB2iiZio9DWW+VTcNiZWiAmagM3DtWPM+PFafHZQz2+3EPPVlAGEvBGi7aEGwCgUNLEzDAXOlkvPq6iNWLIWCGt5WKkIygcjgNPmiiPYTz1F9xQF703lGl+xNS93D/79AoGAZR/MyKZ31U3D2yGqG1LHVj8E9i6mXiA30Oi3+4CwbbdemWGNNn2Z6Iu6d9dCAWMyv5z3QMvj+bFznfp22jVga1YJ/DZOnWWXL1M3v8oUXfHNcZXQFjlI5agtMulRo5Rr8rpmJHuJH15qUX/s9/woeaAdaOLu0EqBka4IfDEtTLECgYANeQ7b+ScjEaT4X3Wgu5kLLjme8WiEGPNFT1vRQ/g0Pd7OAZxB05gH5AjN4Rgv+dIcxsZySSemXsjysi/uzaNq8nX8MS+xBBbhBoSSD41YfLAMwlKHjwMPL4/rHYC9oYteRyj3Op5rA9jE6NgCJdqszdzsNGHY8wR8aTLEu9+tFA==";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public static String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqtFbDvUd2+3KzGwy+eHfdf5VQ2Ui34qBUfQA2LQtnwnqYu+QmkLIYuIYkwXPzBnCCbspKWfcg4ZvjeKGSWn/+fC4l0gWfYtTaqg5R883OlWMHQQ8srkmU90cPNbrmf0FFUXblC94+GUx+CmsUpk1aJEEnwi564a3xsbFNuUFpPwRR778O5B/IxMFKbnuDEhhNhhqM4rHQJcW5QPVJYiqlQvVinYWR9enRpjNKB67WgxdJ4xPHo5aonjMGYnEWmBsAwqXUxo63sdhcXL4ynns3nDcLvNVfP1wTAS3Cr0sde2s6GDeGihM27MpcAWAt0OXA3cLlDme/sMAyBHmGgu73wIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url;

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url;

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();
        //自动关单
        String timeout="30m";

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
